$(function () {
	$("#jqGrid").Grid({
		url: '../energycoin/list',
		colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '邀请人ID', name: 'uid', index: 'uid', width: 80,align:'center'},
			{label: '被邀请人ID', name: 'inviteUid', index: 'invite_uid', width: 80,align:'center'},
			{label: '邀请人手机号', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '被邀请人手机号', name: 'invitePhone', index: 'invite_phone', width: 80,align:'center'},
			{label: '能量币数量', name: 'energyCoin', index: 'energy_coin', width: 80,align:'center'},
			{label: '类型编号', name: 'type', index: 'type', width: 80,
				formatter:function (value) {
                    if(value=="1"){
                        return "普通邀请注册";
                    }
                    if(value=="2"){
                        return "运动得能量值-普通用户";
                    }
                    if(value=="3"){
                        return "获得资质-D-one特权";
                    }
                    if(value=="4"){
                        return "运动得能量值-D-one特权";
                    }
                    if(value=="5"){
                        return "新用户注册";
                    }
                    if(value=="6"){
                        return "D-one邀请注册";
                    }
                    if(value=="7"){
                        return "任务奖励";
                    }
                    if(value=="8"){
                        return "兑换商品";
                    }
                    if(value=="10"){
                        return "商城产购买品";
                    }
                    if(value=="11"){
                        return "普通运动返奖励";
                    }
                    if(value=="12"){
                        return "Done运动返奖励";
                    }
					if(value=="13"){
						return "每日任务-每日签到";
					}
					if(value=="14"){
						return "每日任务-发帖";
					}
					if(value=="15"){
						return "每日任务-评论";
					}
					if(value=="16"){
						return "每日任务-点赞";
					}
					if(value=="17"){
						return "每日任务-精选内容";
					}
					if(value=="18"){
						return "每日任务-跑步打卡分享";
					}
					if(value=="19"){
						return "普通运动";
					}
					if(value=="20"){
						return "Done运动";
					}
					if(value=="21"){
						return "团队运动奖励";
					}
					if(value=="22"){
						return "商城购物赠送";
					}
					if(value=="23"){
						return "推荐D-one";
					}
					if(value=="24"){
						return "成为D-one";
					}
					if(value=="25"){
						return "商城购物释放";
					}
					if(value=="26"){
						return "每日释放";
					}
					if(value=="27"){
						return "Done用户推荐Done-释放";
					}
					if(value=="37"){
						return "Done用户推荐Done-赠送";
					}
					if(value=="28"){
						return "普通用户推荐Done-释放";
					}
					if(value=="38"){
						return "普通用户推荐Done-赠送";
					}
					if(value=="30"){
						return "商城购物-冻结";
					}
					if(value=="31"){
						return "商城购物退款-退还能量";
					}
					if(value=="32"){
						return "商城购物退款-退还冻结";
					}
					return "";
				}
			},
			{label: '类型', name: 'typeName', index: 'type_name', width: 80,align:'center'},
			{label: '状态', name: 'incomeType', index: 'income_type', width: 80,align:'center',
				formatter:function (value) {
					if(value==0){
						return "收入";
					}
					if(value==1){
						return "支出";
					}
				}
			},
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80,align:'center',
				formatter:function (value) {
					return transDate(value, 'yyyy-MM-dd hh:mm:ss');
				}
			}
			]
	});
});

let vm = new Vue({
	el: '#rrapp',
	data: {
		showList: true,
		title: null,
		energyCoin: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
			name: '',
			type:'',
			incomeType:'',
			coins:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.energyCoin = {};
		},
		update: function (event) {
			let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			let url = vm.energyCoin.id == null ? "../energycoin/save" : "../energycoin/update";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.energyCoin),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
		del: function (event) {
			let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				Ajax.request({
					url: "../energycoin/delete",
					params: JSON.stringify(ids),
					type: "POST",
					contentType: "application/json",
					successCallback: function () {
						alert('操作成功', function (index) {
							vm.reload();
						});
					}
				});
			});
		},
		getInfo: function(id){
			Ajax.request({
				url: "../energycoin/info/"+id,
				async: true,
				successCallback: function (r) {
					vm.energyCoin = r.energyCoin;
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData: {'name': vm.q.name,'type':vm.q.type,'incomeType':vm.q.incomeType},
				page: page
			}).trigger("reloadGrid");
			vm.handleReset('formValidate');
			vm.queryCoin();
		},
		reloadSearch: function() {
			vm.q = {
				name: '',
				type:'',
				incomeType:'',
				coins:''
			}
			vm.reload();
		},
		handleSubmit: function (name) {
			handleSubmitValidate(this, name, function () {
				vm.saveOrUpdate()
			});
		},
		handleReset: function (name) {
			handleResetForm(this, name);
		},queryCoin: function () {
			Ajax.request({
				url: "../energycoin/queryAllCoin?name="+vm.q.name+"&type="+vm.q.type+"&incomeType="+vm.q.incomeType,
				async: true,
				successCallback: function (r) {
					console.log(r.coins);
					vm.q.coins = r.coins;
				}
			});
		}
	}
});